package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.up.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.up.HomePageBasicTempMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.up.HomePageBasicTempVoMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.up.HomePageUserTempMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.up.HomePageBasicTemp;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.up.HomePageUserTemp;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.up.HomePageBasicTempService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${yangsy} on 2017/3/23.
 */
@Service
public class HomePageBasicTempServiceImpl implements HomePageBasicTempService {
    private static Logger logger = LoggerFactory.getLogger(HomePageBasicTempServiceImpl.class);
    @Autowired
    private HomePageBasicTempMapper homePageBasicTempMapper;
    @Autowired
    private HomePageBasicTempVoMapper homePageBasicTempVoMapper;
    @Autowired
    private HomePageUserTempMapper homePageUserTempMapper;

    @Override
    public List<HomePageBasicTemp> queryListNotpaging(HomePageBasicTemp homePageBasicTemp) {
        return homePageBasicTempMapper.queryListNotpaging(homePageBasicTemp);
    }

    @Override
    public List<HomePageBasicTemp> queryListNotpading(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        List<HomePageBasicTemp> list = null;
        try {
            list = homePageBasicTempVoMapper.queryList(userId);
        } catch (Exception e) {
            logger.error("读取人员模板配置异常:" + e.getMessage());
        }
        return list;
    }

    @Override
    public Object queryUserTemp(String userId, String pfToken) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        try {
            //   查询当前人是否配置了模板，如果没配置，则显示基础模板配置信息
            List<HomePageUserTemp> userTempList = homePageUserTempMapper.queryList(userId);
            //  没有配置的情况，用基本模板配置信息
            List<HomePageBasicTemp> list = null;
            if (userTempList != null && userTempList.size() > 0) {
                list = homePageBasicTempVoMapper.queryList(userId);
                mapList = operateionData(list, true, pfToken);
            } else {
                list = homePageBasicTempMapper.queryListNotpaging(new HomePageBasicTemp());
                mapList = operateionData(list, false, pfToken);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("读取人员模板配置异常:" + e.getMessage());
        }
        return mapList;
    }

    protected List<Map<String, Object>> operateionData(List<HomePageBasicTemp> list, boolean queryUserTemp, String pfToken) {
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (HomePageBasicTemp temp : list) {
            Map<String, Object> map = new HashMap<>();
            BigDecimal x = temp.getX();
            BigDecimal y = temp.getY();
            BigDecimal width = temp.getWidth();
            BigDecimal height = temp.getHeight();
            boolean isDisplay = true;
            //  读取用户配置表数据
            if (queryUserTemp) {
                if (temp.getUserTemp() != null) {
                    x = temp.getUserTemp().getX();
                    y = temp.getUserTemp().getY();
                    width = temp.getUserTemp().getWidth();
                    height = temp.getUserTemp().getHeight();
                } else {
                    isDisplay = false;
                }
            } else {
                if (temp.getIsDisplay() == 1) {
                    isDisplay = false;
                }
            }
            //  不变信息
            map.put("id", temp.getId());
            map.put("name", temp.getName());
            map.put("icon", temp.getIcon());
            map.put("title", temp.getTitle());
            map.put("url", temp.getUrl());
            boolean isCount = true;
            if (temp.getIsWidgetlr() == 1) {
                isCount = false;
            }
            map.put("isCount", isCount);
            map.put("type", temp.getType());
            map.put("radiusType", temp.getRadiusType());
            String clickEventUrl = "";
            if (StringUtils.isNotEmpty(temp.getClickEventUrl())) {
                //clickEventUrl = temp.getClickEventUrl() + "?pfToken="+ pfToken;
                clickEventUrl = temp.getClickEventUrl();
            }
            map.put("clickEventUrl", clickEventUrl);
            map.put("isDisplay", isDisplay);
            map.put("x", x);
            map.put("y", y);
            map.put("width", width);
            map.put("height", height);

            mapList.add(map);
        }
        return mapList;
    }
}
